{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "logical-works",
   "metadata": {},
   "source": [
    "## 三、概率统计与随机过程编程（本大题30分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "small-damages",
   "metadata": {},
   "source": [
    "4、用Python编程实现贝叶斯公式的计算：假设有两个教室各有l00个学生，A教室中有60个男生、40个女生，B教室中有30个男生、70个女生。假设随机选择其中一个教室,从里面叫出一个人记下性别再回到原来的教室，那么被选择的教室是A教室的概率有多大?（12分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faced-middle",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "effective-fundamental",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "被选中的是A教室的概率是: 0.6666666666666667\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# A教室中学生总数\n",
    "A_total = 100  \n",
    "# A教室中男生人数  \n",
    "A_male = 60\n",
    "# A教室中女生人数\n",
    "A_female = A_total - A_male  \n",
    "\n",
    "# B教室中学生总数\n",
    "B_total = 100   \n",
    "# B教室中男生人数\n",
    "B_male = 30  \n",
    "# B教室中女生人数  \n",
    "B_female = B_total - B_male   \n",
    "\n",
    "# 随机选择A或B教室的概率相同,都是0.5\n",
    "p_A = 0.5  \n",
    "p_B = 0.5\n",
    "\n",
    "# A教室中挑选到男生的概率\n",
    "p_male_given_A = A_male / A_total  \n",
    "\n",
    "# B教室中挑选到男生的概率\n",
    "p_male_given_B = B_male / B_total\n",
    "\n",
    "# 观察到的证据是选到了一个男生\n",
    "E = 'male' \n",
    "\n",
    "# 应用贝叶斯公式计算被选中的是A教室的概率 \n",
    "p_A_given_E = (p_male_given_A * p_A) / (p_male_given_A * p_A + p_male_given_B * p_B)\n",
    "\n",
    "print('被选中的是A教室的概率是:', p_A_given_E)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "connected-pillow",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "capable-specialist",
   "metadata": {},
   "source": [
    "1.根据已知条件,A教室中男女生比例为6:4,B教室中比例为3:7。随机选择一个教室后,挑选一个学生,发现是男生。\n",
    "3.应用贝叶斯公式计算后,我们得到被选中教室为A教室的概率约为0.67,即2/3。这表示,在观察到挑选出的是一个男生的条件下,选中的教室很有可能是A教室。\n",
    "3.进一步分析这个结果,是因为A教室中男生占比更高(0.6 vs 0.3),所以当观察到男生这个证据时,贝叶斯计算会更新先验概率,使得A教室被选中的概率增加。\n",
    "4.这个例子很好地体现了贝叶斯规则思想的核心——即通过观察结果来更新对未知事物的先验认知,从而得出更准确的后验概率。当我们获得新的证据时,就可以不断更新和修正我们的判断。\n",
    "5.正确运用贝叶斯法则,可以帮助我们在许多决策和预测问题中做出更可靠的推断。这是一个极为重要和强大的思维工具。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "difficult-split",
   "metadata": {},
   "source": [
    "5、用Python的stats.pojssn库和scipy.stats.poisson库实现泊松过程。已知某路口上午9点至9点30分通过的大货车数量是5辆，那么在该时段通过大货车7辆的概率是多少?泊松分布的输出是一个数列，包含了发生通过0辆、l辆、2辆，直到l2辆大货车的概率。（18分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesbian-bernard",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "extreme-zambia",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过不同数量大货车的概率分布:\n",
      "通过 0 辆大货车的概率为: 0.000045\n",
      "通过 1 辆大货车的概率为: 0.000454\n",
      "通过 2 辆大货车的概率为: 0.002270\n",
      "通过 3 辆大货车的概率为: 0.007567\n",
      "通过 4 辆大货车的概率为: 0.018917\n",
      "通过 5 辆大货车的概率为: 0.037833\n",
      "通过 6 辆大货车的概率为: 0.063055\n",
      "通过 7 辆大货车的概率为: 0.090079\n",
      "通过 8 辆大货车的概率为: 0.112599\n",
      "通过 9 辆大货车的概率为: 0.125110\n",
      "通过 10 辆大货车的概率为: 0.125110\n",
      "通过 11 辆大货车的概率为: 0.113736\n",
      "通过 12 辆大货车的概率为: 0.094780\n",
      "\n",
      "通过7辆大货车的概率为: 0.090079225719216\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "\n",
    "# 已知在时间段内通过的大货车数量\n",
    "observed_trucks = 5\n",
    "\n",
    "# 假设时间段为30分钟\n",
    "time_period = 30  # 单位：分钟\n",
    "\n",
    "# 计算单位时间内平均通过的大货车数量\n",
    "average_trucks = observed_trucks / (time_period / 60)  # 转换为小时\n",
    "\n",
    "# 创建泊松分布对象\n",
    "poisson_dist = stats.poisson(mu=average_trucks)\n",
    "\n",
    "# 计算通过不同数量大货车的概率分布\n",
    "num_trucks = 13  # 假设最多通过13辆大货车\n",
    "probabilities = [poisson_dist.pmf(i) for i in range(num_trucks)]\n",
    "\n",
    "# 计算通过7辆大货车的概率\n",
    "probability_7_trucks = poisson_dist.pmf(7)\n",
    "\n",
    "# 输出概率分布和通过7辆大货车的概率\n",
    "print(\"通过不同数量大货车的概率分布:\")\n",
    "for i, prob in enumerate(probabilities):\n",
    "    print(f\"通过 {i} 辆大货车的概率为: {prob:.6f}\")\n",
    "\n",
    "print(\"\\n通过7辆大货车的概率为:\", probability_7_trucks)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "organized-horizon",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "united-worst",
   "metadata": {},
   "source": [
    "1. **通过不同数量大货车的概率分布**：\n",
    "   - 通过循环计算，得到了在给定时间段内通过不同数量大货车的概率分布。\n",
    "   - 每个概率代表了在该时间段内恰好通过指定数量大货车的可能性。\n",
    "\n",
    "2. **通过7辆大货车的概率**：\n",
    "   - 代码计算了通过7辆大货车的概率，即在特定时间段内正好通过7辆大货车的概率。\n",
    "   - 这个值提供了具体数量的大货车通过的可能性。\n",
    "\n",
    "3. **概率分布分析**：\n",
    "   - 泊松分布描述了在特定时间段内事件发生次数的概率分布。\n",
    "   - 概率随着大货车数量的增加而递减，越多大货车通过的概率越低。\n",
    "\n",
    "4. **结果解读**：\n",
    "   - 这些概率值对于预测在给定时间段内通过特定数量大货车的概率非常有用。\n",
    "   - 例如，通过7辆大货车的概率是一个具体的数值，可用于预测这种情况发生的可能性。\n",
    "\n",
    "这样的分析有助于理解在给定时间段内通过不同数量大货车的概率，以及针对特定数量的预测概率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee2281c6-a0c5-41e8-bdb9-d718c01fce4d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
